package org.totoro.structure.stack.impl;

import org.totoro.structure.list.impl.ArrayList;
import org.totoro.structure.stack.Stack;

/**
 * 数组实现的栈
 * <p>
 * 先进后出 特性
 *
 * @author daocr
 * @date 2020/3/22
 */
public class ArrayStack<E> implements Stack<E> {

    private ArrayList<E> list;

    public ArrayStack() {
        list = new ArrayList<>();
    }

    @Override
    public void push(E e) {
        list.add(e);
    }

    @Override
    public E pop() {
        E last = list.getLast();
        list.remove(last);
        return last;
    }

    @Override
    public E peek() {
        E last = list.getLast();
        return last;
    }

    @Override
    public int getSize() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    public static void main(String[] args) {

        ArrayStack<String> arrayStack = new ArrayStack<>();

        for (int i = 0; i < 100; i++) {
            arrayStack.push(String.valueOf(i));
        }

        String element= null;

        while ((element =arrayStack.pop()) != null){
            System.out.println(element);
        }


    }
}
